import { Component, OnInit } from '@angular/core';
import { LayoutRourteStatus } from 'src/app/model/status';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { MemberService } from 'src/app/routes/member-center/member.server';
import { MessageService } from 'src/app/core/service';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { Router } from '@angular/router';

@Component({
    selector: 'app-edit-jy-psw',
    templateUrl: './edit-jy-psw.component.html',
    styleUrls: ['./edit-jy-psw.component.scss']
})
export class EditJyPswComponent implements OnInit {

    public resetPwsForm: FormGroup;
    public editPwsForm: FormGroup;
    public isFundPwd: boolean;
    public isHowToShow: number;//0:用戶沒開通交易密碼；1：用戶忘記交易密碼； 2：用戶已經開通交易密碼需修改密碼
    public authCodeLable = '发送验证码';
    public authCodeLableType = false;
    private user: any;
    public isLoading = false;
    private _layoutRourteStatus: LayoutRourteStatus


    ngOnInit() {
        this.user = this.storageService.get('wenyao-web-fallet-user-info');
        const info = this.memberServer.getRouterInfo('type');
        if (this.user) {
            this.isFundPwd =  this.user['isFundPwd'];
            (this.isFundPwd && !info) ? this.editPwd() : this.resetPwd(info);
            this.msg.sendLayoutRourteStatus(this._layoutRourteStatus);
        }
    }

    public resetPwd(info): void {
        let title = '重置支付密码';
        if (info) {
            this.isHowToShow = 1;
            title = '重置支付密码';
        } else {
            this.isHowToShow = 0;
            title = '重置支付密码';
        }
        this._layoutRourteStatus = {
            url: '/member/center/account',
            isHiddeBreadcrumb: false,
            breadcrumbs: [
                { tragetTitle: '安全设置', breadcrumbUrl: '/member/center/account/setting' },
                { tragetTitle: title }
            ]
        };
        this.resetPwsForm = this.fb.group({
            verifyCode: [null, [Validators.required]],
            newFundPwd: [null, [Validators.required]],
            newConfirmFundPwd: [null, [Validators.required, this.confirmationValidator]],
        });
    }
    public editPwd(): void {
        this.isHowToShow = 2;
        this._layoutRourteStatus = {
            url: '/member/center/account',
            isHiddeBreadcrumb: false,
            breadcrumbs: [
                { tragetTitle: '安全设置', breadcrumbUrl: '/member/center/account/setting' },
                { tragetTitle: '修改支付密码' }
            ]
        };
        this.editPwsForm = this.fb.group({
            oldFundPwd: [null, [Validators.required]],
            newFundPwd: [null, [Validators.required]],
            newConfirmFundPwd: [null, [Validators.required, this.confirmationEditValidator]],
        });

    }

    public submit(data) {
        this.isHowToShow == 2 ? this.submitAjx('商户修改交易密码(已登陆)', data) : this.submitAjx('商户重置交易密码(已登陆)', data);
    }
    async submitAjx(url, data) {
        if (url == '商户重置交易密码(已登陆)') {
            let verifyData = {
                email: this.user.loginId,
                emailVerifyType: "refundpwd",
                verifyCode: data.verifyCode
            };
            const tag = await this.memberServer.getAJX('验证码验证', verifyData);
            let isSubmin = this.memberServer.getApiSuccess(tag, false);
            if (isSubmin) {
                const tag = await this.memberServer.getAJX(url, data);
                this.memberServer.getApiSuccess(tag, { bool: true, msg: '支付密码重置成功！' }) ? this.successListOk(tag, url) : null;
            }
        } else {
            const tag = await this.memberServer.getAJX(url, data);
            this.memberServer.getApiSuccess(tag, { bool: true, msg: '支付密码修改成功！' }) ? this.successListOk(tag, url) : null;
        }
    }
    public successListOk(res, url): void {
        switch (url) {
            case '商户修改交易密码(已登陆)':
                this._router.navigate(['/member/center/account/setting']);
                break;
            case '商户重置交易密码(已登陆)':
                this._router.navigate(['/member/center/home']);
                break;
            case '重置交易密码时(已登入) 发送验证码':
                this.getAuthCodeLable();
                break;
        }
    }
    confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
        if (!control.value) {
            return { required: true };
        } else if (control.value !== this.resetPwsForm.controls.newFundPwd.value) {
            return { confirm: true, error: true };
        }
    }
    confirmationEditValidator = (control: FormControl): { [s: string]: boolean } => {
        if (!control.value) {
            return { required: true };
        } else if (control.value !== this.editPwsForm.controls.newFundPwd.value) {
            return { confirm: true, error: true };
        }
    }
    getAuthCodeLable() {
        this.authCodeLableType = true;
        if (this.authCodeLable === '发送验证码' || this.authCodeLable === '重新发送') {
            let num = 60;
            this.authCodeLable = '重新发送(' + num + 's)';
            const timmer = setInterval(() => {
                num--;
                this.authCodeLable = '重新发送(' + num + 's)';
            }, 1000);
            setTimeout(() => {
                clearInterval(timmer);
                this.authCodeLableType = false;
                this.authCodeLable = '重新发送';
            }, 60 * 1000);
        }
    }
    private getVerifyCode(): void {
        if (!this.authCodeLableType) {
            this.isLoading = true;
            this.toGetVerifyCode({ email: this.user.loginId });
        }
    }
    async toGetVerifyCode(data) {
        const tag = await this.memberServer.getAJX('重置交易密码时(已登入) 发送验证码', data);
        this.isLoading = false;
        this.memberServer.getApiSuccess(tag, { bool: true, msg: '验证码发送成功，请注意查收！' }) ? this.successListOk(tag, '重置交易密码时(已登入) 发送验证码') : null;
    }

    constructor(
        private fb: FormBuilder,
        private memberServer: MemberService,
        private storageService: StorageService,
        private _router: Router,
        private msg: MessageService
    ) {


    }
}
